﻿using GameFramework;
using UnityGameFramework.Runtime;
using GameFramework.Event;

namespace GFTest
{
    /// <summary>
    /// NGUI 界面抽象类。
    /// </summary>
    [XLua.LuaCallCSharp]
    public abstract class NGUIForm : UIFormLogic
    {
        public const int DepthFactor = 100;

        private UIPanel m_CachedUIPanel = null;

        private UIEffectsController m_UIEffectsController = null;

        private UIFormParams m_UIFormParams = null;

        public int OriginalDepth
        {
            get;
            private set;
        }

        public int Depth
        {
            get
            {
                return m_CachedUIPanel.depth;
            }
        }

        public void Close()
        {
            GameEntry.UI.CloseUIForm(this);
        }

        public void PlayUISound(int uiSoundId)
        {
            //zph modify
            //GameEntry.Sound.PlayUISound(uiSoundId);
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnInit(object userData)
#else

        protected internal override void OnInit(object userData)
#endif
        {
            base.OnInit(userData);

            m_CachedUIPanel = GetComponent<UIPanel>();
            if (m_CachedUIPanel == null)
            {
                Log.Error("UIPanel for '{0}' is invalid.", Name);
                return;
            }

            m_UIEffectsController = GetComponent<UIEffectsController>();

            OriginalDepth = m_CachedUIPanel.depth;

            UILabel[] uiLabels = GetComponentsInChildren<UILabel>(true);
            for (int i = 0; i < uiLabels.Length; i++)
            {
                if (!string.IsNullOrEmpty(uiLabels[i].text))
                {
                    uiLabels[i].text = GameEntry.Localization.GetString(uiLabels[i].text);
                }
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnOpen(object userData)
#else

        protected internal override void OnOpen(object userData)
#endif
        {
            base.OnOpen(userData);

            if (m_UIEffectsController != null)
            {
                m_UIEffectsController.Show();
            }

            if (userData != null)
            {
                m_UIFormParams = (UIFormParams)userData;
                int soundId = m_UIFormParams.UIData.OpenSound;
                if (soundId != 0)
                {
                    PlayUISound(soundId);
                }
            }

            //监听临时隐藏及打开UI事件
            GameEntry.Event.Subscribe(HideUIEventArgs.EventId, OnHideUI);
            GameEntry.Event.Subscribe(ShowUIEventArgs.EventId, OnShowUI);
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnClose(object userData)
#else

        protected internal override void OnClose(object userData)
#endif
        {
            base.OnClose(userData);

            if (m_UIEffectsController != null)
            {
                m_UIEffectsController.Pause();
            }

            if (m_UIFormParams != null)
            {
                int soundId = m_UIFormParams.UIData.CloseSound;
                if (soundId != 0)
                {
                    PlayUISound(soundId);
                }

                ReferencePool.Release(m_UIFormParams);
            }
            GameEntry.Event.Unsubscribe(HideUIEventArgs.EventId, OnHideUI);
            GameEntry.Event.Unsubscribe(ShowUIEventArgs.EventId, OnShowUI);
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnPause()
#else

        protected internal override void OnPause()
#endif
        {
            base.OnPause();

            if (m_UIEffectsController != null)
            {
                m_UIEffectsController.Pause();
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnResume()
#else

        protected internal override void OnResume()
#endif
        {
            base.OnResume();

            if (m_UIEffectsController != null)
            {
                m_UIEffectsController.Resume();
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnCover()
#else

        protected internal override void OnCover()
#endif
        {
            base.OnCover();
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnReveal()
#else

        protected internal override void OnReveal()
#endif
        {
            base.OnReveal();
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnRefocus(object userData)
#else

        protected internal override void OnRefocus(object userData)
#endif
        {
            base.OnRefocus(userData);

            if (m_UIEffectsController != null)
            {
                m_UIEffectsController.Resume();
            }
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
#else

        protected internal override void OnUpdate(float elapseSeconds, float realElapseSeconds)
#endif
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);
        }

#if UNITY_2017_3_OR_NEWER

        protected override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
#else

        protected internal override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
#endif
        {
            int oldDepth = Depth;
            base.OnDepthChanged(uiGroupDepth, depthInUIGroup);

            int deltaDepth = NGUIGroupHelper.DepthFactor * uiGroupDepth + DepthFactor * depthInUIGroup - oldDepth + OriginalDepth;
            UIPanel[] panels = GetComponentsInChildren<UIPanel>(true);
            for (int i = 0; i < panels.Length; i++)
            {
                panels[i].depth += deltaDepth;
            }
        }

        protected void OnHideUI(object sender, GameEventArgs e)
        {
            HideUIEventArgs eventArgs = e as HideUIEventArgs;
            if (eventArgs != null)
            {
                if (eventArgs.Mode == HideUIEventArgs.HideMode.Default)
                {
                    if (eventArgs.ParamList.Contains(UIForm.SerialId))
                    {
                        gameObject.SetActive(false);
                    }
                }
                else if (eventArgs.Mode == HideUIEventArgs.HideMode.Except)
                {
                    if (!eventArgs.ParamList.Contains(UIForm.SerialId))
                    {
                        gameObject.SetActive(false);
                    }
                }
            }
        }

        protected void OnShowUI(object sender, GameEventArgs e)
        {
            ShowUIEventArgs eventArgs = e as ShowUIEventArgs;
            if (eventArgs != null)
            {
                if (eventArgs.Mode == ShowUIEventArgs.ShowMode.Default)
                {
                    if (eventArgs.ParamList.Contains(UIForm.SerialId))
                    {
                        gameObject.SetActive(true);
                    }
                }
                else if (eventArgs.Mode == ShowUIEventArgs.ShowMode.Except)
                {
                    if (!eventArgs.ParamList.Contains(UIForm.SerialId))
                    {
                        gameObject.SetActive(true);
                    }
                }
            }
        }
    }
}
